package com.jiepos.mpos.biz.customer.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.CustomAccountDao;
import com.jiepos.mpos.common.entity.App_Custom_AccountEntity;
import com.jiepos.mpos.common.entity.App_Custom_InfEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 会员账户业务类
 * 
 * @ProjectName: [mpos1]
 * @Package com.jiepos.mpos.biz.customer.service
 * @author 肖志伟
 * @version 1.0
 * @since 2017年9月25日 上午11:50:59
 * @history（历次修订内容、修订人、修订时间等） @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
@Service
public class CustomAccountService extends BaseService {

	@Autowired
	private CustomAccountDao customAccountDao;

	/**
	 * 列表页获取会员账户信息
	 * 
	 * @param request
	 * 
	 * @param pagination
	 * @param customInfEntity
	 * @param appCustomAccountEntity
	 * @param ins_cd
	 * @param ins_name
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map getCustomerAccountList(HttpServletRequest request, Pagination pagination,
			App_Custom_InfEntity customInfEntity, App_Custom_AccountEntity appCustomAccountEntity, String ins_cd,
			String ins_name) {
		StringBuffer sb = getInitSql("t1").append(", ");
		sb.append("t1.account_uuid as UUID, t1.custom_uuid, t1.account_no, ");
		sb.append("t2.custom_no, ");
		sb.append("CONCAT (RPAD (SUBSTR (t2.login_id, 1, 3),LENGTH (t2.login_id) - 4,'*'),SUBSTR (t2.login_id,LENGTH (t2.login_id) - 3,4))||'-'||t2.name as name, ");
		// 账户类型name
		sb.append("(select code_val||'-'||code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = '"
				+ DBConstant.Code.ACCOUNT_TYPE + "' and code_val = t1.account_type) as account_type_name, ");
		sb.append("t1.directions, ");
		sb.append("to_char(t1.balance,'fm9999999990.00') as balance, "); // 余额
		sb.append("t1.account_st, ");
		sb.append("(select code_val||'-'||code_desc from " + Table.T_APP_CODE_LIST + " where code_tp = '"
				+ DBConstant.Code.ACCOUNT_ST + "' and code_val = t1.account_st) as account_st_name, ");
		sb.append("to_char(t1.cash_balance,'fm9999999990.00') as cash_balance, ");// 已支取金额
		sb.append("to_char(t1.have_balance,'fm9999999990.00') as have_balace, "); // 可用余额
		sb.append("t3.ins_cd||'-'||t3.ins_cn_nm as ins_cn_nm "); // 机构名
		sb.append("from "); // 机构名
		sb.append(Table.T_APP_CUSTOM_ACCOUNT);
		sb.append(" t1 left join ");
		sb.append(Table.T_APP_CUSTOM_INF);
		sb.append(" t2 on t1.custom_uuid = t2.custom_uuid ");
		sb.append("left join ");
		sb.append(Table.T_APP_INS_INF);
		sb.append(" t3 on t3.ins_uuid = t2.ins_uuid ");
		sb.append("where 1=1 ");
		List list = new ArrayList();
		// 会员手机号
		if (!StringUtils.isBlank(customInfEntity.getPhone())) {
			sb.append(" and t2.login_id like ? ");
			list.add("%" + customInfEntity.getPhone() + "%");
		}
		// 会员名称
		if (!StringUtils.isBlank(customInfEntity.getName())) {
			sb.append(" and t2.name like ? ");
			list.add("%" + customInfEntity.getName() + "%");
		}
		// 账户状态
		if (!StringUtils.isBlank(appCustomAccountEntity.getAccount_st())) {
			sb.append(" and t1.account_st = ? ");
			list.add(appCustomAccountEntity.getAccount_st());
		}
		// 机构号
		if (!StringUtils.isBlank(ins_cd)) {
			sb.append(" and t3.ins_cd = ? ");
			list.add(ins_cd);
		}
		// 机构名称
		if (!StringUtils.isBlank(ins_name)) {
			sb.append(" and t3.ins_cn_nm like ? ");
			list.add("%" + ins_name + "%");
		}
		// 会员编号
		if (!StringUtils.isBlank(customInfEntity.getCustom_no())) {
			sb.append(" and t2.custom_no like ? ");
			list.add("%" + customInfEntity.getCustom_no() + "%");
		}
		// 账户类型
		if (!StringUtils.isBlank(appCustomAccountEntity.getAccount_type())) {
			sb.append(" and t1.Account_type = ? ");
			list.add(appCustomAccountEntity.getAccount_type());
		}

		pagination = super.getCustomAuthSql(request, pagination, "t1", null);
		//pagination = this.getOrderSql(pagination, "t1");
		sb.append(" order by t1.cash_balance desc ");
		pagination.queryString = sb.toString();
		return customAccountDao.getPaginationMap(pagination, "t1", list);
	}

	/**
	 * 修改会员账户状态
	 * 
	 * @param account_uuids
	 * @param loginUser
	 * @param appCustomAccountEntity
	 */
	public void updateCustomAccountSt(String account_uuids, Map loginUser,
			App_Custom_AccountEntity appCustomAccountEntity) {
		App_Custom_AccountEntity appCustomAccountEn = (App_Custom_AccountEntity) super.saveCommonData(
				appCustomAccountEntity, loginUser, "update");
		customAccountDao.updateCustomAccountStByIds("account_uuid", appCustomAccountEn, account_uuids);
		/* 调用存储过程修改DAC */
		customAccountDao.updateCustomAccountProcedure(account_uuids);

	}

	/**
	 * 根据会员UUID得到会员账户余额数据
	 * 
	 * @param custom_uuid
	 * @return
	 */
	public String getCustomAccountBalance(String custom_uuid) {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" SELECT TO_CHAR(NVL(SUM(BALANCE),0.00),'fm9999999990.00') AS BALANCE ")
				.append("   FROM  " + Table.T_APP_CUSTOM_ACCOUNT).append("   WHERE REC_ST != '0' ")
				.append("   AND CUSTOM_UUID = ? ")
				.append("   AND ACCOUNT_TYPE IN ('" + SystemConstant.ACCOUNT_TYPE_3 + "'")
				.append(",'" + SystemConstant.ACCOUNT_TYPE_4 + "'")
				.append(",'" + SystemConstant.ACCOUNT_TYPE_2 + "') ");
		List<String> params = new ArrayList<>();
		params.add(custom_uuid);
		List<Map<String, Object>> list = customAccountDao.queryForList(sqlBuf.toString(), params);
		if (list != null && list.size() > 0 && list.get(0) != null && list.get(0).get("BALANCE") != null) {
			return String.valueOf(list.get(0).get("BALANCE"));
		} else {
			return "0.00";
		}
	}

	/**
	 * 推荐佣金和可提现推荐佣金
	 * 
	 * @param custom_uuid
	 * @return
	 */
	public List getRecmdCommission(String custom_uuid) {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select  ").append(" TO_CHAR(NVL(t1.BALANCE,0.00),'fm9999999990.00') AS recmd_commission, ")// 推荐佣金
				.append(" TO_CHAR(NVL(t1.HAVE_BALANCE,0.00),'fm9999999990.00') AS recmd_withdraw_commission ")// 可提现推荐佣金
				.append("   from " + Table.T_APP_CUSTOM_ACCOUNT + " t1 ")
				.append("  where T1.CUSTOM_UUID = ? AND T1.ACCOUNT_TYPE = " + SystemConstant.ACCOUNT_TYPE_2);
		List params = new ArrayList();
		params.add(custom_uuid);
		List<Map<String, Object>> list = customAccountDao.queryForList(sqlBuf.toString(), params);
		Map map = new HashMap();
		if (CollectionUtil.isEmpty(list)) {
			map.put("recmd_commission", "0.00");
			map.put("recmd_withdraw_commission", "0.00");
			list.add(map);
			return list;
		}
		return list;
	}

	/**
	 * 充值佣金和可提现充值佣金
	 * 
	 * @param custom_uuid
	 * @return
	 */
	public List getCommission(String custom_uuid) {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select  ").append(" TO_CHAR(NVL(t1.BALANCE,0.00),'fm9999999990.00') AS commission, ")// 充值佣金
				.append(" TO_CHAR(NVL(t1.HAVE_BALANCE,0.00),'fm9999999990.00') AS withdraw_commission ")// 可提现充值佣金
				.append("   from " + Table.T_APP_CUSTOM_ACCOUNT + " t1 ")
				.append("  where T1.CUSTOM_UUID = ? AND T1.ACCOUNT_TYPE = " + SystemConstant.ACCOUNT_TYPE_3);
		List params = new ArrayList();
		params.add(custom_uuid);
		List<Map<String, Object>> list = customAccountDao.queryForList(sqlBuf.toString(), params);
		Map map = new HashMap();
		if (CollectionUtil.isEmpty(list)) {
			map.put("commission", "0.00");
			map.put("withdraw_commission", "0.00");
			list.add(map);
			return list;
		}
		return list;
	}
	/**
	 * 总余额
	 * 
	 * @param custom_uuid
	 * @return
	 *//*
		 * public List getAllAmt(String custom_uuid) { StringBuffer sqlBuf = new
		 * StringBuffer(); sqlBuf.append(" select  ")
		 * .append(" TO_CHAR(NVL(SUM(t1.BALANCE),0.00),'fm9999999990.00') AS all_amt "
		 * )//余额 .append("   from " + Table.T_APP_CUSTOM_ACCOUNT + " t1 ")
		 * .append("  where T1.CUSTOM_UUID = ? "); List params = new
		 * ArrayList(); params.add(custom_uuid); List<Map<String, Object>> list
		 * = customAccountDao.queryForList(sqlBuf.toString(), params); Map map =
		 * new HashMap(); if (CollectionUtil.isEmpty(list)) {
		 * map.put("all_amt","0.00"); list.add(map); return list; } return list;
		 * }
		 */

	/**
	 * 查询分润余额
	 * 
	 * @param custom_uuid
	 * @return
	 */
	public List getProfitBalance(String custom_uuid) {
		StringBuffer sqlBuf = new StringBuffer();
		sqlBuf.append(" select  ").append(" TO_CHAR(NVL(t1.BALANCE,0.00),'fm9999999990.00') AS profit, ")// 分润余额
				.append(" TO_CHAR(NVL(t1.HAVE_BALANCE,0.00),'fm9999999990.00') AS withdraw_profit ")// 可提现分润余额
				.append("   from " + Table.T_APP_CUSTOM_ACCOUNT + " t1 ")
				.append("  where T1.CUSTOM_UUID = ? AND T1.ACCOUNT_TYPE = " + SystemConstant.ACCOUNT_TYPE_4);
		List params = new ArrayList();
		params.add(custom_uuid);
		List<Map<String, Object>> list = customAccountDao.queryForList(sqlBuf.toString(), params);
		Map map = new HashMap();
		if (CollectionUtil.isEmpty(list)) {
			map.put("profit", "0.00");
			map.put("withdraw_profit", "0.00");
			list.add(map);
			return list;
		}
		return list;
	}
}
